//
//  BKCMRecommendCell.m
//  bestkeep
//
//  Created by 毛韶谦 on 2017/1/16.
//  Copyright © 2017年 utouu. All rights reserved.
//BUYCARGRAYCOLOR

#import "BKCMRecommendCell.h"
#import "BKCMGoodsDetailModel.h"
#import "BKSCCommon.h"


#define kColorDetailLabel RGB(64, 64, 64)
#define kColorGrayLabel HexColor(0xB2B2B2)

@interface BKCMRecommendCell ()

@property (strong, nonatomic) UIView *recommendMainRihgtView;   //主View；
@property (strong, nonatomic) UIView *recommendMainLeftView;
@property (strong, nonatomic) UIImageView *recommendMainLeftImageView;  //主图；
@property (strong, nonatomic) UIImageView *recommendMainRightImageView;
@property (strong, nonatomic) UIImageView *recommendGlobalLeftImageView;  //全球购标志；
@property (strong, nonatomic) UIImageView *recommendGlobalRightImageView;
@property (strong, nonatomic) UILabel *recommendLeftTitleLabel;  //标题；
@property (strong, nonatomic) UILabel *recommendRightTitleLabel;
@property (strong, nonatomic) UILabel *recommendLeftPriceLabel;   //价格label；
@property (strong, nonatomic) UILabel *recommendRightPriceLabel;
@property (strong, nonatomic) UILabel *recommendRightMarkerPriceLabel;  //市场价格
@property (strong, nonatomic) UILabel *recommendLeftMarkerPriceLabel;

@property (strong, nonatomic) UIImageView *recommendLeftGoodStatus;
@property (strong, nonatomic) UIImageView *recommendRightGoodStatus;//商品状态；
@property (strong, nonatomic) UIImageView *recommendRightLine;
@end

@implementation BKCMRecommendCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self configTabelViewCell];
    }
    return self;
}
- (void)configTabelViewCell {
    
    self.recommendMainLeftView = [[UIView alloc] init];
    self.recommendMainRihgtView = [[UIView alloc] init];
    [self  addSubview:self.recommendMainRihgtView];
    [self  addSubview:self.recommendMainLeftView];
    
    [self.recommendMainLeftView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self .mas_top);
        make.bottom.mas_equalTo(self .mas_bottom);
        make.left.mas_equalTo(self .mas_left);
        make.right.mas_equalTo(self.recommendMainRihgtView.mas_left);
    }];
    [self.recommendMainRihgtView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self .mas_top);
        make.bottom.mas_equalTo(self .mas_bottom);
        make.right.mas_equalTo(self .mas_right);
        make.width.mas_equalTo(self.recommendMainLeftView.mas_width);
    }];
    //设置线条
    _recommendRightLine = [[UIImageView alloc] init];
    UIImageView *recommendLeftLine = [[UIImageView alloc] init];
    UIImageView *recommendMiddleLine = [[UIImageView alloc] init];
    [self.recommendMainLeftView addSubview:recommendMiddleLine];
    [self.recommendMainLeftView addSubview:recommendLeftLine];
    [self.recommendMainRihgtView addSubview:_recommendRightLine];
    _recommendRightLine.backgroundColor = HexColor(0xDADADA);
    recommendLeftLine.backgroundColor = HexColor(0xDADADA);
    recommendMiddleLine.backgroundColor = HexColor(0xDADADA);
    [recommendLeftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.recommendMainLeftView.mas_bottom);
        make.left.mas_equalTo(self.recommendMainLeftView.mas_left);
        make.right.mas_equalTo(self.recommendMainLeftView.mas_right);
        make.height.mas_equalTo(0.5f);
    }];
    [_recommendRightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.recommendMainLeftView.mas_bottom);
        make.left.mas_equalTo(self.recommendMainRihgtView.mas_left);
        make.right.mas_equalTo(self.recommendMainRihgtView.mas_right);
        make.height.mas_equalTo(0.5f);
    }];
    [recommendMiddleLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.recommendMainLeftView.mas_top);
        make.bottom.mas_equalTo(self.recommendMainLeftView.mas_bottom);
        make.left.mas_equalTo(self.recommendMainLeftView.mas_right).offset(-0.25);
        make.width.mas_equalTo(0.5f);
    }];

    //市场价格
    self.recommendLeftMarkerPriceLabel = [[UILabel alloc] init];
    self.recommendLeftMarkerPriceLabel.font = [UIFont systemFontOfSize:12];
    self.recommendLeftMarkerPriceLabel.textColor = kColorGrayLabel;
    [self.recommendLeftMarkerPriceLabel setTextAlignment:NSTextAlignmentLeft];
    [self.recommendMainLeftView addSubview:self.recommendLeftMarkerPriceLabel];
    
    self.recommendRightMarkerPriceLabel = [[UILabel alloc] init];
    self.recommendRightMarkerPriceLabel.font = [UIFont systemFontOfSize:12];
    self.recommendRightMarkerPriceLabel.textColor = kColorGrayLabel;
    [self.recommendRightMarkerPriceLabel setTextAlignment:NSTextAlignmentLeft];
    [self.recommendMainRihgtView addSubview:self.recommendRightMarkerPriceLabel];
    
    self.recommendLeftTitleLabel = [[UILabel alloc] init];
    self.recommendLeftTitleLabel.textColor = kColorDetailLabel;
    self.recommendLeftTitleLabel.font = [UIFont systemFontOfSize:13];
    self.recommendLeftTitleLabel.numberOfLines = 1;
    [self.recommendMainLeftView addSubview:self.recommendLeftTitleLabel];
    self.recommendLeftPriceLabel = [[UILabel alloc] init];
    self.recommendLeftPriceLabel.font = [UIFont systemFontOfSize:16];
    self.recommendLeftPriceLabel.textColor = BESTKEEP_RED;
    [self.recommendMainLeftView addSubview:self.recommendLeftPriceLabel];
    self.recommendMainLeftImageView = [[UIImageView alloc] init];
    [self.recommendMainLeftView addSubview:self.recommendMainLeftImageView];
    
    [self.recommendLeftPriceLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.recommendMainLeftView.mas_left).mas_offset(ApplicationBackGauge);
        make.bottom.mas_equalTo(self.recommendMainLeftView.mas_bottom).mas_offset(-10);
        make.height.mas_equalTo(15);
    }];
    
    [self.recommendLeftTitleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.recommendMainLeftImageView.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(self.recommendMainLeftView.mas_left).mas_offset(ApplicationBackGauge);
        make.right.mas_equalTo(self.recommendMainLeftView.mas_right).mas_offset(-ApplicationBackGauge);
        make.height.mas_equalTo(14);
    }];
    
    [self.recommendMainLeftImageView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.recommendMainLeftView.mas_bottom).mas_offset(-60);
        make.top.mas_equalTo(self.recommendMainLeftView.mas_top).mas_offset(10);
        make.centerX.mas_equalTo(self.recommendMainLeftView.mas_centerX);
        make.width.equalTo(self.recommendMainLeftImageView.mas_height);
    }];
    
    self.recommendRightTitleLabel = [[UILabel alloc] init];
    self.recommendRightTitleLabel.font = [UIFont systemFontOfSize:13];
    self.recommendRightTitleLabel.textColor = kColorDetailLabel;
    self.recommendRightTitleLabel.numberOfLines = 1;
    [self.recommendMainRihgtView addSubview:self.recommendRightTitleLabel];
    self.recommendRightPriceLabel = [[UILabel alloc] init];
    self.recommendRightPriceLabel.font = [UIFont systemFontOfSize:16];
    self.recommendRightPriceLabel.textColor = BESTKEEP_RED;
    [self.recommendMainRihgtView addSubview:self.recommendRightPriceLabel];
    self.recommendMainRightImageView = [[UIImageView alloc] init];
    [self.recommendMainRihgtView addSubview:self.recommendMainRightImageView];
    /*
     *
     *
     *  @param make <#make description#>
     *
     *  @return <#return value description#>
     */
    [self.recommendRightPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.recommendMainRihgtView.mas_left).mas_offset(ApplicationBackGauge);
        make.bottom.mas_equalTo(self.recommendMainRihgtView.mas_bottom).mas_offset(-10);
        make.height.mas_equalTo(15);
    }];
    
    [self.recommendRightTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.recommendMainRightImageView.mas_bottom).mas_offset(15);
        make.left.mas_equalTo(self.recommendMainRihgtView.mas_left).mas_offset(ApplicationBackGauge);
        make.right.mas_equalTo(self.recommendMainRihgtView.mas_right).mas_offset(-ApplicationBackGauge);
        make.height.mas_equalTo(14);
    }];
    
    [self.recommendMainRightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.recommendMainRihgtView.mas_top).mas_offset(10);
        make.bottom.mas_equalTo(self.recommendMainRihgtView.mas_bottom).mas_offset(-60);
        make.centerX.mas_equalTo(self.recommendMainRihgtView.mas_centerX);
        make.width.mas_equalTo(self.recommendMainRightImageView.mas_height);
    }];
    
    self.recommendLeftGoodStatus = [[UIImageView alloc] init];
    self.recommendRightGoodStatus = [[UIImageView alloc] init];
    [self.recommendMainLeftImageView addSubview:self.recommendLeftGoodStatus];
    [self.recommendMainRightImageView addSubview:self.recommendRightGoodStatus];
    [self.recommendLeftGoodStatus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.recommendMainLeftView.mas_left);
        make.top.mas_equalTo(self.recommendMainLeftView.mas_top);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
    [self.recommendRightGoodStatus mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.recommendMainRihgtView.mas_left);
        make.top.mas_equalTo(self.recommendMainRihgtView.mas_top);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(40);
    }];
    
    self.recommendGlobalLeftImageView = [[UIImageView alloc] init];
    [self.recommendMainLeftView addSubview:self.recommendGlobalLeftImageView];
    [self.recommendGlobalLeftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.recommendLeftTitleLabel.mas_top).offset(-1.2);
        make.left.mas_equalTo(self.recommendLeftTitleLabel.mas_left);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(13);
    }];
    self.recommendGlobalRightImageView = [[UIImageView alloc] init];
    [self.recommendMainRihgtView addSubview:self.recommendGlobalRightImageView];
    [self.recommendGlobalRightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.recommendRightTitleLabel.mas_top).offset(-1.2);
        make.left.mas_equalTo(self.recommendRightTitleLabel.mas_left);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(13);
    }];
    
    self.recommendRightGoodStatus.hidden = YES;
    self.recommendLeftGoodStatus.hidden = YES;
    self.recommendMainLeftImageView.userInteractionEnabled = YES;
    self.recommendMainRightImageView.userInteractionEnabled = YES;
    [self.recommendMainRightImageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.recommendMainLeftImageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.recommendGlobalLeftImageView setContentMode:UIViewContentModeScaleAspectFit];
    [self.recommendGlobalRightImageView setContentMode:UIViewContentModeScaleAspectFit];
    self.recommendMainLeftView.backgroundColor = [UIColor whiteColor];
    self.recommendMainRihgtView.backgroundColor = [UIColor whiteColor];
    
}

- (void)refreshDataToInterfaceLeft:(BKCMGoodsDetailModel *)model {
    
    self.recommendLeftTitleLabel.text = model.goods_name;
    
    if ([model.channelType isEqualToString:@"2"]) {
        self.recommendGlobalLeftImageView.hidden = NO;
        self.recommendGlobalLeftImageView.image = [UIImage imageNamed:@"BKSCCrossBorder"];
        //首行缩进
        [self setFirstLineTextIndentWithContent:self.recommendLeftTitleLabel firstLineHeadIndent:45];
    }else {
        self.recommendGlobalLeftImageView.hidden = YES;
        [self setFirstLineTextIndentWithContent:self.recommendLeftTitleLabel firstLineHeadIndent:0];
    }
    if ([model.status isEqualToString:@"4"]) {
        self.recommendLeftGoodStatus.hidden = NO;
        self.recommendLeftGoodStatus.image = [UIImage imageNamed:@"BKSCStates"];
    }else {
        self.recommendLeftGoodStatus.hidden = YES;
    }
    [self.recommendMainLeftImageView sd_setImageWithURL:[NSURL URLWithString:model.goods_img] placeholderImage:[UIImage imageNamed:@"BKSCpic_newGoods"]];
    
    float price = [[NSString stringWithFormat:@"%@",model.goods_price] floatValue];//会员价；
    self.recommendLeftPriceLabel.text = [NSString stringWithFormat:@"¥%.2f%@",price,model.upText];
    [self setMutabelAttributedString:self.recommendLeftPriceLabel];
    float marketPrice = [[NSString stringWithFormat:@"%@",model.goodsMarketPrice] floatValue];//市场价
    //显示下划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥%.2f",marketPrice] attributes:attribtDic];
    self.recommendLeftMarkerPriceLabel.attributedText = attribtStr;
    CGFloat width = [self getLabelSize:16 string: self.recommendLeftPriceLabel.text].width;
    
    width = (IPHONE_WIDTH)/2 - width - ApplicationBackGauge*2 - 5;
    if ([model.upText isEqualToString:@"起"]) {
        width += 4;
    }
    
    [self.recommendLeftMarkerPriceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.bottom.mas_equalTo(self.recommendLeftPriceLabel.mas_bottom).mas_offset(0);
        make.right.mas_equalTo(self.recommendMainLeftView).mas_offset(-ApplicationBackGauge);
        make.height.mas_equalTo(12);
    }];
    
}

- (void)refreshDataToInterfaceRight:(BKCMGoodsDetailModel *)model isOdd:(BOOL)isOdd{
    
    self.recommendRightTitleLabel.text = model.goods_name;
    
    if (isOdd) {
        self.recommendMainRihgtView.backgroundColor = BUYCARGRAYCOLOR;
        self.recommendGlobalRightImageView.hidden = YES;
        self.recommendRightGoodStatus.hidden = YES;
        self.recommendMainRightImageView.hidden = YES;
        self.recommendRightPriceLabel.hidden = YES;
        self.recommendRightMarkerPriceLabel.hidden = YES;
        self.recommendRightTitleLabel.hidden = YES;
        _recommendRightLine.hidden = YES;
        return;
    }else {
        self.recommendMainRihgtView.backgroundColor = [UIColor whiteColor];
        self.recommendMainRihgtView.hidden = NO;
        self.recommendGlobalRightImageView.hidden = NO;
        self.recommendRightGoodStatus.hidden = NO;
        self.recommendMainRightImageView.hidden = NO;
        self.recommendRightPriceLabel.hidden = NO;
        self.recommendRightMarkerPriceLabel.hidden = NO;
        self.recommendRightTitleLabel.hidden = NO;
        _recommendRightLine.hidden = NO;
    }
    if ([model.channelType isEqualToString:@"2"]) {
        self.recommendGlobalRightImageView.hidden = NO;
        self.recommendGlobalRightImageView.image = [UIImage imageNamed:@"BKSCCrossBorder"];
        [self setFirstLineTextIndentWithContent:self.recommendRightTitleLabel firstLineHeadIndent:45];
        
    }else {
        self.recommendGlobalRightImageView.hidden = YES;
        [self setFirstLineTextIndentWithContent:self.recommendRightTitleLabel firstLineHeadIndent:0];
    }
    if ([model.status isEqualToString:@"4"]) {
        self.recommendRightGoodStatus.hidden = NO;
        self.recommendRightGoodStatus.image = [UIImage imageNamed:@"BKSCStates"];
    }else {
        self.recommendRightGoodStatus.hidden = YES;
    }
    [self.recommendMainRightImageView sd_setImageWithURL:[NSURL URLWithString:model.goods_img] placeholderImage:[UIImage imageNamed:@"BKSCpic_newGoods"]];
    float price = [[NSString stringWithFormat:@"%@",model.goods_price] floatValue];
    self.recommendRightPriceLabel.text = [NSString stringWithFormat:@"¥%.2f%@",price,model.upText];
    [self setMutabelAttributedString:self.recommendRightPriceLabel];
    float marketPrice = [[NSString stringWithFormat:@"%@",model.goodsMarketPrice] floatValue];//市场价
    //显示下划线
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"¥%.2f",marketPrice] attributes:attribtDic];
    self.recommendRightMarkerPriceLabel.attributedText = attribtStr;
    
    CGFloat width = [self getLabelSize:16 string: self.recommendRightPriceLabel.text].width;
    width = (IPHONE_WIDTH)/2 - width - ApplicationBackGauge*2 - 5;
    if ([model.upText isEqualToString:@"起"]) {
        width += 4;
    }
    
    [self.recommendRightMarkerPriceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.bottom.mas_equalTo(self.recommendRightPriceLabel.mas_bottom).mas_offset(0);
        make.right.mas_equalTo(self.recommendMainRihgtView).mas_offset(-ApplicationBackGauge);
        make.height.mas_equalTo(12);
    }];
}

/**
 *  处理Controller 中的事件
 *
 *  @param recommendCell      注册的cell
 *  @param recommendGoodsList 数据数组
 *  @param indexPath          cell位置
 *  @param isOdd              是否是双数
 */
- (void)dealRecommendTabelViewCell:(BKCMRecommendCell *)recommendCell recommendGoodsList:(NSMutableArray *)recommendGoodsList indexPath:(NSIndexPath *)indexPath isOdd:(BOOL)isOdd {
    
    if (isOdd) {
        [recommendCell refreshDataToInterfaceLeft:[recommendGoodsList objectAtIndex:indexPath.row*2]];
        if (recommendGoodsList.count/2 != indexPath.row) {
            [recommendCell refreshDataToInterfaceRight:[recommendGoodsList objectAtIndex:indexPath.row*2+1]isOdd:NO];
        }else {
            [recommendCell refreshDataToInterfaceRight:[recommendGoodsList objectAtIndex:indexPath.row*2] isOdd:YES];
        }
    }else {
        [recommendCell refreshDataToInterfaceLeft:[recommendGoodsList objectAtIndex:indexPath.row*2]];
        [recommendCell refreshDataToInterfaceRight:[recommendGoodsList objectAtIndex:indexPath.row*2+1] isOdd:NO];
    }
}

//设置首行缩进
-(void)setFirstLineTextIndentWithContent:(UILabel*)contentLabel firstLineHeadIndent:(CGFloat)value
{
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:contentLabel.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3];
    paragraphStyle.firstLineHeadIndent= value;
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [contentLabel.text length])];
    contentLabel.attributedText = attributedString;
    contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    
}
//设置“起”的富文本展现
- (void)setMutabelAttributedString:(UILabel *)contentLabel {
    
    NSString *string = contentLabel.text;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    
    if ([string hasSuffix:@"起"]) {
        
        [attributedString setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName,BESTKEEP_RED,NSForegroundColorAttributeName,nil] range:NSMakeRange(0, string.length-1)];
        [attributedString setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12],NSFontAttributeName,kColorGrayLabel,NSForegroundColorAttributeName,nil] range:NSMakeRange(string.length-1, 1)];
    }else {
        [attributedString setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName,BESTKEEP_RED,NSForegroundColorAttributeName,nil] range:NSMakeRange(0, string.length)];
    }
    
    contentLabel.attributedText = attributedString;
    contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
}

//计算label长度
- (CGSize)getLabelSize:(CGFloat)font string:(NSString *)string {
    
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:font],};
    
    CGSize textSize = [string boundingRectWithSize:CGSizeMake(100000, 100) options:NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;;
    
    return textSize;
}



- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
